package cn.com.transfer.listener;

import cn.com.transfer.utils.Constants;
import cn.com.transfer.utils.IcbcNative;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.IocException;
import org.nutz.ioc.impl.NutIoc;
import org.nutz.ioc.loader.json.JsonLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2015/4/3.
 */
public class TransferServer implements ServletContextListener {
    private Logger logger = LoggerFactory.getLogger(TransferServer.class);
    private Ioc ioc;
    private Server server;

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        String dir = servletContextEvent.getServletContext().getRealPath("/");
        File configFile = new File(dir + File.separator + "config.js");
        try {
            ioc = new NutIoc(new JsonLoader(configFile.getPath()));
            //启动服务
            server = ioc.get(Server.class, "server");
            server.start();
        } catch (IocException e) {
            logger.error("error in start service", e);
        }
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        server.stop();
        if (ioc != null) {
            ioc.depose();
        }
    }
}
